import wx, { Component, PropTypes } from 'labrador-immutable';
import { bindActionCreators } from 'redux';
import { connect } from 'labrador-redux';
import { createAction } from 'redux-actions';
import Resource from '../../components/resource/resource';

class Index extends Component {

    state = {
        goodId: ''
    }

    children() {
        let productResource = this.props.productResource || [];
        return {
            productResource: productResource.map((resource, index) => ({
                component: Resource,
                key: index,
                props: {
                    ...resource
                }
            })),
        }
    }

    onLoad(options) {
        this.setState({ goodId: options.id, fromType: options.fromType })
        this.props.fetchProductResource({ goodId: options.id });
    }

    // 设置分享
    onShareAppMessage() {
        return {
            title: this.props.name,
            desc: '萌酱酱选辅食一个致力于帮助新手爸妈为宝宝轻松制作辅食的APP。',
            path: '/pages/resourcelist/index?goodId=' + this.state.goodId + '&fromType=share',
            imageUrl: this.props.thumbnail
        }
    }

    backtoIndex(e) {
        this.props.uploadFormId({ form_id: e.detail.formId });
        wx.switchTab({
            url: '/pages/index/index'
        });
    }
}

export default connect(
    ({ resource }) => ({ productResource: resource.productResource, name: resource.name, thumbnail: resource.thumbnail }),
    (dispatch) => bindActionCreators({
        fetchProductResource: createAction('resource/fetchProductResource', (params) => (params)),
        uploadFormId: createAction('user/uploadFormId', (params) => (params)),
    }, dispatch)
)(Index);